/*
 * Copyright (c) 2008, Takashi Ito
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the authors nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef _TDOM_INTERNAL_H_
#define _TDOM_INTERNAL_H_

#include "config.h"

#ifdef WIN32
#define _CRT_SECURE_NO_WARNINGS
#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#include <windows.h>
#include <conio.h>
#undef WIN32_LEAN_AND_MEAN
#undef SLIST_ENTRY
#endif

#if STDC_HEADERS
#include <stdio.h>
#endif

#if HAVE_STRING_H
#include <string.h>
#endif 
#if HAVE_STDLIB_H
#include <stdlib.h>
#endif
#if HAVE_STDINT_H
#include <stdint.h>
#endif

#if HAVE_SYS_QUEUE_H
#include <sys/queue.h>
#else
#include "sys/queue.h"
#endif

#if HAVE_EXPAT_H
#include <expat.h>
#endif
#if HAVE_EVENT_H
#include <event.h>
#endif

#ifdef HAVE_ASSERT_H
#include <assert.h>
#define tdom_assert assert
#else /* HAVE_ASSERT_H */
#define tdom_assert(a)
#endif /* HAVE_ASSERT_H */

#ifdef WIN32
#define strcasecmp  _stricmp
#define snprintf    _snprintf
#define strdup      _strdup
#endif /* WIN32 */

#ifdef TDOM_TEST
char *__strdup(const char *s);
#ifndef strdup
#define strdup __strdup
#else
#define _strdup __strdup
#endif
#endif

#define TAILQ_FREE_ALL(var, head, field, handler) do { \
    while ((var = TAILQ_FIRST(head))) { \
        TAILQ_REMOVE(head, var, field); \
        handler(var); \
    } \
} while (/*CONSTCOND*/0)

#define TAILQ_FOREACH_2(var1, var2, head1, head2, field) \
    for ((var1) = TAILQ_FIRST((head1)), (var2) = TAILQ_FIRST((head2)); \
         (var1) || (var2); \
         (var1) = (var1) ? TAILQ_NEXT((var1), field) : NULL, \
         (var2) = (var2) ? TAILQ_NEXT((var2), field) : NULL) \

#define max(a, b) (((a) > (b)) ? (a) : (b))
#define min(a, b) (((a) < (b)) ? (a) : (b))

#define TDOM_GEN_PREFIX_MAX 100

#endif /* _TDOM_INTERNAL_H_ */

